{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "6a3cb0b2-dfdf-44fa-be15-464d956702fe",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The autoreload extension is already loaded. To reload it, use:\n",
      "  %reload_ext autoreload\n"
     ]
    }
   ],
   "source": [
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "2f130140-9970-4d75-89a5-71587860d558",
   "metadata": {},
   "outputs": [],
   "source": [
    "from jsputils import paths, classes, nsdorg, plotting, encoding, nnutils, selectivity, eiganalysis, gpu_encoding\n",
    "import os\n",
    "from os.path import exists\n",
    "import cortex\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from fastprogress import progress_bar\n",
    "import scipy.stats as stats\n",
    "from scipy.spatial.distance import pdist, squareform\n",
    "import copy\n",
    "import time\n",
    "import gc\n",
    "from IPython.core.debugger import set_trace\n",
    "import pandas as pd\n",
    "import seaborn as sns\n",
    "from sklearn.decomposition import PCA\n",
    "from PIL import Image\n",
    "\n",
    "import sys\n",
    "sys.path.append('/home/jovyan/work/DropboxSandbox/GSN')\n",
    "import gsn\n",
    "from gsn.rsa_noise_ceiling import rsa_noise_ceiling\n",
    "\n",
    "import torch\n",
    "import torchvision\n",
    "import torchlens as tl\n",
    "\n",
    "from torchvision.transforms._presets import ImageClassification\n",
    "\n",
    "from sklearn.linear_model import Lasso, LinearRegression"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "58d6bf01-d14e-4f29-b185-f40ded4eab0c",
   "metadata": {},
   "outputs": [],
   "source": [
    "nsddir = paths.nsd()\n",
    "\n",
    "feature_savedir = f'{os.getcwd()}/analysis_outputs/3d-LowLevel'\n",
    "encoding_savedir =  f'{os.getcwd()}/analysis_outputs/3-Encoding'\n",
    "\n",
    "train_imageset = 'nonshared1000-3rep-batch0'\n",
    "val_imageset = 'nonshared1000-3rep-batch1'\n",
    "test_imageset = 'special515'\n",
    "\n",
    "subjs = [f'subj0{s}' for s in range(1,9)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "83450288-b551-4590-983b-ce672d6fadd4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       "    /* Turns off some styling */\n",
       "    progress {\n",
       "        /* gets rid of default border in Firefox and Opera. */\n",
       "        border: none;\n",
       "        /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
       "        background-size: auto;\n",
       "    }\n",
       "    progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
       "        background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
       "    }\n",
       "    .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
       "        background: #F44336;\n",
       "    }\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      <progress value='8' class='' max='8' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      100.00% [8/8 01:37&lt;00:00]\n",
       "    </div>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n",
      "skipping, already exists\n"
     ]
    }
   ],
   "source": [
    "for subj in progress_bar(subjs):\n",
    "    \n",
    "    subj_image_data = nsdorg.get_NSD_train_test_images(subj, train_imageset, test_imageset)\n",
    "    \n",
    "    if subj == 'subj01':\n",
    "        \n",
    "        savefn = f'{feature_savedir}/special515_images.mat'\n",
    "        if not exists(savefn):\n",
    "            # Save it as a .mat file\n",
    "            sio.savemat(savefn, {'images': subj_image_data['test']})\n",
    "            #np.save(f'{savedir}/special515_images.npy', subj_image_data['test'])\n",
    "            print(subj_image_data['test'].shape)\n",
    "        else:\n",
    "            print('skipping, already exists')\n",
    "    \n",
    "    savefn = f'{feature_savedir}/{subj}_train-{train_imageset}_images.mat'\n",
    "    if not exists(savefn):\n",
    "        sio.savemat(savefn, {'images': subj_image_data['train']})\n",
    "        #np.save(savefn, subj_image_data['train'])\n",
    "        print(subj, subj_image_data['train'].shape)\n",
    "    else:\n",
    "        print('skipping, already exists')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "8a149991-85e1-46c6-9b6e-10e8f790808f",
   "metadata": {},
   "outputs": [],
   "source": [
    "roi_list = ['FFA-1','FFA-2','OFA','PPA','OPA','EBA','FBA-1','FBA-2','VWFA-1','VWFA-2','OWFA']\n",
    "\n",
    "train_imageset = 'nonshared1000-3rep-batch0'\n",
    "val_imageset = 'nonshared1000-3rep-batch1'\n",
    "test_imageset = 'special515'\n",
    "\n",
    "space = 'nativesurface'\n",
    "beta_version = 'betas_fithrf_GLMdenoise_RR'\n",
    "ncsnr_threshold = 0.3\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "4fc87ce8-bd2e-4d3a-b544-8c10cf7816f2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(515, 50)\n"
     ]
    }
   ],
   "source": [
    "def load_gist_gabor_features(feature_savedir, subj, ft):\n",
    "\n",
    "\n",
    "    if not 'subj' in subj:\n",
    "        struct = sio.loadmat(f'{feature_savedir}/{test_imageset}_{ft}.mat')\n",
    "    else:\n",
    "        struct = sio.loadmat(f'{feature_savedir}/{subj}_{ft}.mat')\n",
    "\n",
    "    # Access the struct as a dictionary\n",
    "    struct = struct[ft]\n",
    "\n",
    "    # If the .mat file was saved in MATLAB with the -v7.3 flag,\n",
    "    # the loaded data is a structured numpy array. To convert it into a dictionary:\n",
    "    if not isinstance(struct, dict):\n",
    "        struct = {n: struct[n][0,0] for n in struct.dtype.names}\n",
    "    \n",
    "    return struct['featureMatrix']\n",
    "\n",
    "print(load_gist_gabor_features(feature_savedir, 'special515','GistPC').shape)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1b644ce6-be1d-4c96-a489-f9fa22dfd176",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "\n",
       "<style>\n",
       "    /* Turns off some styling */\n",
       "    progress {\n",
       "        /* gets rid of default border in Firefox and Opera. */\n",
       "        border: none;\n",
       "        /* Needs to be in here for Safari polyfill so background images work as expected. */\n",
       "        background-size: auto;\n",
       "    }\n",
       "    progress:not([value]), progress:not([value])::-webkit-progress-bar {\n",
       "        background: repeating-linear-gradient(45deg, #7e7e7e, #7e7e7e 10px, #5c5c5c 10px, #5c5c5c 20px);\n",
       "    }\n",
       "    .progress-bar-interrupted, .progress-bar-interrupted::-webkit-progress-bar {\n",
       "        background: #F44336;\n",
       "    }\n",
       "</style>\n"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "text/html": [
       "\n",
       "    <div>\n",
       "      <progress value='1' class='' max='8' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
       "      12.50% [1/8 00:19&lt;02:16]\n",
       "    </div>\n",
       "    "
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "betas already saved. loading...\n",
      "loading train, val, and test data\n",
      "subj01 PPA GistPC\n",
      "\tpreparing encoding brain data...\n",
      "\t\t train (1000, 3907)\n",
      "\t\tmasking\n",
      "\t\t train (1000, 3097)\n",
      "\t\t test (515, 3907)\n",
      "\t\tmasking\n",
      "\t\t test (515, 3097)\n",
      "\tpreparing encoding model features... GistPC\n",
      "\tfitting model... LinearRegression()\n",
      "\tgenerating predictions...\n",
      "\tcomputing metrics...\n",
      "\t\t\t test cUnivar 0.019 cRSA 0.041 veUnivar 0.02 veRSA 0.036\n",
      "\tsaving\n",
      "betas already saved. loading...\n",
      "loading train, val, and test data\n"
     ]
    }
   ],
   "source": [
    "results = dict()\n",
    "\n",
    "for ft in ['GistPC','Gabor']:\n",
    "    \n",
    "    results[ft] = dict()\n",
    "    \n",
    "    test_features = load_gist_gabor_features(feature_savedir, 'special515', ft)\n",
    "\n",
    "    for roi in roi_list:\n",
    "\n",
    "        results[ft][roi] = dict()\n",
    "\n",
    "        for subj in progress_bar(subjs):\n",
    "            NSDsubj = classes.fMRISubject(subj, space, beta_version)\n",
    "            ROI = classes.BrainRegion(NSDsubj, roi)\n",
    "\n",
    "            ROI.load_betas()\n",
    "            ROI.get_ncsnr_mask(threshold = ncsnr_threshold)\n",
    "            ROI.load_encoding_data(train_imageset, val_imageset, test_imageset)\n",
    "            \n",
    "            train_features = load_gist_gabor_features(feature_savedir, subj, ft)\n",
    "\n",
    "            encoder = classes.EncodingProcedureGistGabor(ROI, ft, \n",
    "                                                         train_features, \n",
    "                                                         test_features,\n",
    "                                                         method = 'ols', \n",
    "                                                         positive = False,\n",
    "                                                         alphas = [None])  # 0.001 for untrained. 0.1 for trained\n",
    "\n",
    "\n",
    "\n",
    "            encoder.encode_features(encoding_savedir, \n",
    "                                  overwrite = False)\n",
    "\n",
    "            results[ft][roi][subj] = encoder.results_df"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "dnffa",
   "language": "python",
   "name": "dnffa"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
